package com.dxy.shardingjdbc.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dxy.shardingjdbc.model.po.Dict;
import com.dxy.shardingjdbc.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典表(Dict)表控制层
 *
 * @author dangxianyue
 * @since 2023-02-09 23:44:47
 */
@Api(tags = "字典接口")
@RestController
@RequestMapping("dict")
public class DictController {

    @Resource
    private DictService dictService;

    @ApiOperation("新增字典")
    @PostMapping("/save")
    public Dict save(String type, String code, String name) {
        Dict dict = new Dict().setType(type).setCode(code).setName(name);
        dictService.save(dict);
        return dict;
    }

    @ApiOperation("根据type查找")
    @GetMapping("/findByType")
    public List<Dict> findByType(String type) {
        return dictService.list(new LambdaQueryWrapper<Dict>().eq(Dict::getType, type));
    }

    @ApiOperation("根据id更新")
    @PostMapping("/updateById")
    public Dict updateById(Dict dict) {
        dictService.updateById(dict);
        return dict;
    }

    @ApiOperation("根据id删除")
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") Long id) {
        dictService.removeById(id);
        return "OK";
    }
}

